package com.ft.oa.service.dev.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ft.oa.domain.dev.DevTask;
import com.ft.oa.domain.dev.DevTaskItem;
import com.ft.oa.domain.dto.ApiResult;
import com.ft.oa.mapper.dev.DevTaskItemMapper;
import com.ft.oa.mapper.dev.DevTaskMapper;
import com.ft.oa.service.dev.DevTaskItemService;
import com.ft.oa.utils.SecurityUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Author: yihui
 * Date: 2022/1/18 10:15 AM
 * FileName: DevTaskItemServiceImpl
 * Description: 研发任务进展
 */
@Transactional
@Service
public class DevTaskItemServiceImpl implements DevTaskItemService {

    @Resource
    DevTaskItemMapper devTaskItemMapper;

    @Resource
    DevTaskMapper devTaskMapper;

    @Override
    public ApiResult add(DevTaskItem devTaskItem) {
        DevTask devTask = devTaskMapper.selectById(devTaskItem.getDevTaskId());
        Map queryMap = new HashMap();
        queryMap.put("dev_task_id",devTaskItem.getDevTaskId());
        List<DevTaskItem> devTaskItemList = devTaskItemMapper.selectByMap(queryMap);
        if (devTaskItemList.size() == 0){
            devTask.setActualStartDate(LocalDate.now());
        }
        if(100 == devTaskItem.getProgressRate()){
            devTask.setTaskStatus(2);
            devTask.setActualFinishDate(LocalDate.now());
        }else{
            devTask.setTaskStatus(1);
        }
        devTask.setProgressRate(devTaskItem.getProgressRate());
        devTaskMapper.updateById(devTask);
        //根据当前进展更新任务进度
        devTaskItem.setCreateDate(LocalDateTime.now());
        Long createStaffId = SecurityUtils.getCurrentUser().getId();
        devTaskItem.setCreateStaffId(createStaffId);
        devTaskItemMapper.insert(devTaskItem);
        return ApiResult.simpleSuccess();
    }

}
